package demo;

import java.nio.charset.StandardCharsets;

public class Demo07 {
    public static void main(String[] args) throws  Exception{
        /*
         * UTF-8 编解码实验
         */
        System.out.println("Unicode 编码为 UTF-8");
        String str = "Aα好\uD83D\uDE02";
        System.out.println(str);
        //UTF-8编码: 1 + 2 + 3 + 4 = 10字节
        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        for (int b : bytes){
            System.out.print(Integer.toBinaryString(b & 0xff) + " ");
        }
        System.out.println();
        //UTF-8 解码
        String s = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(s);
        //乱码：文字编码方案和解码方案不一致，会照成乱码
        s = new String(bytes, "GBK");
        System.out.println(s);
    }
}
